package com.java46.carera.stock.controller;

import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.entity.Repertory;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.util.JsonUtil;
import com.java46.carera.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName StockController
 * @Description TODO
 * @Author Administrator
 * @Date 2018/8/22 0022 9:59
 * @Vcrsion 1.0
 **/
@Controller
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockService stockService;


    /**
     * @Author xiao
     * @Description //分页查询可根据关键字
     * @Date 10:46 2018/8/22 0022
     * @Param [pagenum, pagesize, keywords]
     * @return com.java46.carera.base.entity.JsonResult
     */
    @RequestMapping("/querystock")
    @ResponseBody
    public JsonResult queryStock(int pagenum,int pagesize,String keywords) throws Exception {

        PageInfo<Repertory> repertoryPageInfo = stockService.queryStock(pagenum, pagesize, keywords);
        JsonResult jsonResult = JsonUtil.setJsonResult(true, "查询成功", repertoryPageInfo);

        return jsonResult;
    }

    /**
     * @Author xiao
     * @Description //通过库存编号查询库存记录
     * @Date 16:38 2018/8/23 0023
     * @Param [repid]
     * @return com.java46.carera.base.entity.JsonResult
     */
    @RequestMapping("/queryStockByRepId")
    @ResponseBody
    public JsonResult queryStockByRepId(int repid) throws Exception {

        Repertory repertory= stockService.queryStockByRepId(repid);
        JsonResult jsonResult = JsonUtil.setJsonResult(true, "查询成功", repertory);

        return jsonResult;
    }

    /**
     * @Author xiao
     * @Description //新增配件
     * @Date 19:35 2018/8/22 0022
     * @Param [repertory]
     * @return com.java46.carera.base.entity.JsonResult
     */
    @RequestMapping("/insertstock")
    @ResponseBody
    public JsonResult insertStock(Repertory repertory) throws Exception {

        stockService.insertStock(repertory);
        JsonResult jsonResult = JsonUtil.setJsonResult(true, "新增成功", null);
        return jsonResult;
    }

    /**
     * @Author xiao
     * @Description //删除库存记录
     * @Date 12:45 2018/8/23 0023
     * @Param [repid]
     * @return com.java46.carera.base.entity.JsonResult
     */
    @RequestMapping("/deletestock")
    @ResponseBody
    public JsonResult deleteStock(Integer repid) throws Exception {

        stockService.deleteByRepId(repid);
        JsonResult jsonResult = JsonUtil.setJsonResult(true, "删除成功", null);
        return jsonResult;
    }

    /**
     * @Author xiao
     * @Description //修改库存记录
     * @Date 16:27 2018/8/23 0023
     * @Param [repertory]
     * @return com.java46.carera.base.entity.JsonResult
     */
    @RequestMapping("/updatestock")
    @ResponseBody
    public JsonResult updateStock(Repertory repertory) throws Exception {

        stockService.updateStock(repertory);
        JsonResult jsonResult = JsonUtil.setJsonResult(true, "修改成功", null);
        return jsonResult;
    }
}
